import {_decorator, Button, Component, director, Label, ProgressBar, resources, Sprite, SpriteFrame, tween} from 'cc';
import {userInfo} from "db://assets/script/user-info";
import {AssetId} from "db://assets/proto/shared/v1/props";
import Long from "long";
import {GameEvent} from "db://assets/script/game-event";
import {configsHelper} from "db://assets/script/configs-helper";
import {textUtils} from "db://assets/script/utils/text-utils";
import {lobby} from "db://assets/script/lobby";
import {socket} from "db://assets/script/common/socket";

const {ccclass, property} = _decorator;

@ccclass('TopBg')
export class TopBg extends Component {

    @property(Label)
    moneyLabel!: Label;
    @property(Label)
    workerLabel!: Label;
    @property(Label)
    woodLabel!: Label;
    @property(Label)
    stoneLabel!: Label;
    @property(Label)
    nameLabel!: Label;

    @property(Button)
    settingsBtn!: Button;

    @property(ProgressBar)
    playerExpBar!: ProgressBar;

    @property(Label)
    expLabel!: Label;

    @property(Sprite)
    playerLevelIcon!: Sprite;


    start() {
        director.on(GameEvent.assetCoreChangeComplete, this.updateAssetUI, this)
        director.on(GameEvent.cityDataChange, this.updateWorker, this)
        this.settingsBtn.node.on(Button.EventType.CLICK, () => {
            lobby.instance.addUIWithClickMask('prefab/common/settings-plane', 0, (node) => node.destroy())
        })
        this.nameLabel.string = userInfo.name
        this.updateMoneyUI()
        this.updateLevelUI()
        this.updateWookUI()
        this.updateStoneUI()
        this.updateWorker()
    }

    updateAssetUI(changedAsset: { [key: number]: Long }) {
        if (changedAsset[AssetId.asset_id_exp]) {
            this.updateLevelUI()
        }
        if (changedAsset[AssetId.asset_id_gold]) {
            this.updateMoneyUI()
        }
        if (changedAsset[AssetId.asset_id_materials_4]) {
            this.updateWookUI()
        }
        if (changedAsset[AssetId.asset_id_materials_5]) {
            this.updateStoneUI()
        }
    }

    updateWorker() {
        const wn = userInfo.cityInfo.loadBuildings(1).filter(it => it.completeAtMs.gt(0)).length
        this.updateWorkerUI(wn)
    }

    updateLevelUI() {
        let exp = userInfo.assetInfo.assets[AssetId.asset_id_exp] || Long.fromNumber(0)
        const level = userInfo.loadUserLevel()
        const lc = configsHelper.loadUserLevelConfig(level + 1)!
        this.playerLevelIcon.spriteFrame = resources.get('textures/ui-base/base-num-' + level + '/spriteFrame', SpriteFrame)
        const progress = exp.toNumber() / lc.requireExp.toNumber()
        console.log('exp progress', progress)
        tween(this.playerExpBar).to(0.5, {progress: progress}, {easing: "circOut"}).start()
        this.expLabel.string = '威望：' + exp.toNumber() + '/' + lc.requireExp.toNumber()
    }

    updateMoneyUI() {
        let money = userInfo.assetInfo.assets[AssetId.asset_id_gold]
        if (!money) money = Long.fromNumber(0)
        this.moneyLabel.string = textUtils.numberToTenThousand(money.toNumber(), 2)
    }


    updateWookUI() {
        let wood = userInfo.assetInfo.assets[AssetId.asset_id_materials_4]
        if (!wood) wood = Long.fromNumber(0)
        this.woodLabel.string = textUtils.numberToTenThousand(wood.toNumber(), 2)
    }

    updateStoneUI() {
        let stone = userInfo.assetInfo.assets[AssetId.asset_id_materials_5]
        if (!stone) stone = Long.fromNumber(0)
        this.stoneLabel.string = textUtils.numberToTenThousand(stone.toNumber(), 2)
    }

    updateWorkerUI(num: number) {
        const level = userInfo.loadUserLevel()
        const lc = configsHelper.loadUserLevelConfig(level)!
        this.workerLabel.string = (lc.workerNum - num) + '/' + lc.workerNum
    }
}